package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Objects;
import com.hitqz.robot.api.business.dto.RobotEquipmentControlButtonDto;
import com.hitqz.robot.api.business.dto.RobotEquipmentPaneButtonsDto;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.common.entity.Button;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.mapper.RobotEquipmentMapper;
import com.hitqz.robot.biz.service.ButtonService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@AllArgsConstructor
public class RobotEquipmentServiceIml extends ServiceImpl<RobotEquipmentMapper, RobotEquipment> implements RobotEquipmentService {

    final private ButtonService buttonService;

    @Override
    public List<RobotEquipmentPaneButtonsDto> getControlButtonList() {
        List<RobotEquipmentPaneButtonsDto> panels = new ArrayList<>();
        List<RobotEquipment> list = this.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getShowPanel, 1));
        for (RobotEquipment robotEquipment : CollUtil.emptyIfNull(list)) {
            Integer type = robotEquipment.getType();
            List<RobotEquipmentControlButtonDto> buttons = new ArrayList<>();
            //根据设备类型获取按钮
            EquipmentEnum[] values = EquipmentEnum.values();
            for (EquipmentEnum value : values) {
                if (Objects.equal(value.getType(), type)){
                    List<Button> buttonDb = buttonService.list(Wrappers.<Button>lambdaQuery().eq(Button::getObject, value.getDes()).eq(Button::getIsShow, true));
                    List<RobotEquipmentControlButtonDto> dtos = BeanUtil.copyToList(buttonDb, RobotEquipmentControlButtonDto.class);
                    if (CollUtil.isNotEmpty(dtos)) {
                        //设置参数
                        dtos.forEach(b->b.setParam(JSON.toJSONString(robotEquipment)));
                        buttons.addAll(dtos);
                    }
                    break;
                }
            }
            if(!buttons.isEmpty()){
                RobotEquipmentPaneButtonsDto panel = new RobotEquipmentPaneButtonsDto();
                panel.setTitle(robotEquipment.getName()+"操作面板");
                panel.setButtons(buttons);
                panels.add(panel);
            }
        }
        return panels;
    }





    private RobotEquipmentControlButtonDto createButton(String name, String action, String param, String picUrl, String picUrlP, String picUrlS,boolean isLongClick) {
        RobotEquipmentControlButtonDto button = new RobotEquipmentControlButtonDto();
        button.setName(name);
        button.setAction(action);
        button.setLongClick(isLongClick);
        button.setPostType("post");
        button.setParam(param);
        button.setDataType("json");
        button.setShowBtnPicUrl(picUrl);
        button.setClickedBtnPicUrl(picUrlP);
        button.setIconNotAllow(picUrlS);
        button.setMapAction("");
        return button;
    }
}
